<template>
  <div class="opinion_main">
    <head-top head-title="意见反馈" goBack="true"></head-top>
    <div class="text_content">
      <textarea @keyup="writeContent" @input="writeContent" name="" id="" placeholder="请输入您的问题(字数不超过200)" cols="30"
                rows="10" v-model="oponions"
                maxlength="200"></textarea>
      <div class="count">
        {{currentCount}}/{{totalCount-currentCount}}
      </div>
    </div>
    <div class="btn_row">
      <button @click="Submits()">
        <span>提交</span>
      </button>
    </div>
    <div class="opinion_title">
      <span>常见问题</span>
    </div>
    <ul>
      <li class="border-bottom" v-for="(list,index) in lists" :key="index">
        <router-link :to="'/my/OpinionDetail/'+list.id">
          <div class="content_box">
            <span>{{list.content}}</span>
          </div>
        </router-link>
      </li>
    </ul>
  </div>
</template>
<style lang="scss">
  .opinion_main {
    width: 100%;
    background: #fff;
    padding-top: 40px;
    .opinion_title {
      width: 100%;
      height: 44px;
      line-height: 44px;
      background: #f2f2f2;
      span {
        font-size: 12px;
        margin-left: 10px;
      }
    }
    ul {
      li {
        height: 40px;
        line-height: 40px;
        margin: 0 10px;
        .content_box {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          color: #393939;
          span {
            font-size: 15px;
          }
        }
      }
    }
    .text_content {
      padding: 10px;
      position: relative;
      .mint-cell:first-child .mint-cell-wrapper {
        background-color: #f2f2f2;
        border-radius: 6px;
        .mint-field-core {
          resize: none;
          background-color: #f2f2f2;
          font-size: 14px;
        }
      }
      textarea {
        box-sizing: border-box;
        padding: 10px;
        width: 100%;
        height: 120px;
        background: #f2f2f2;
        border-radius: 5px;
        resize: none;
        outline: none;
        border: none;
      }
      .count {
        position: absolute;
        bottom: 20px;
        right: 20px;
        color: #0078de;
      }
    }
    .btn_row {
      width: 100%;
      text-align: center;
      button {
        width: 90%;
        height: 44px;
        background: #0078de;
        outline: none;
        border: none;
        border-radius: 5px;
        margin-top: 10px;
        margin-bottom: 20px;
        span {
          font-size: 15px;
          color: #fff;
        }
      }
    }
  }
</style>
<script>
  import headTop from '../header/Header.vue'
  import qs from 'qs'
  import commonUrl from '../../common/js/commonUrl.js'
  import { Toast } from 'mint-ui'

  export default {
    name: 'Opinion',
    data () {
      return {
        oponions: '',
        lists: [],
        totalCount: 200,
        currentCount: 0
      }
    },
    components: {
      headTop
    },
    created () {
      this.getOPinion()
    },
    methods: {
      writeContent () {
        this.currentCount = this.oponions.length
        let remainCount = this.totalCount - this.currentCount
        if (!remainCount) {
          Toast({
            message: '字数不能超过200',
            position: 'middle',
            duration: 1000
          })
        }
      },
      // 提交意见
      Submits () {
        if (!this.oponions.trim().length || !this.oponions) {
          Toast({
            message: '请输入要提问的内容',
            position: 'middle',
            duration: 2000
          })
          return
        }
        const url = `${commonUrl.apihost}index.php/home/personal/suggest/`
        const data = {
          content: this.oponions
        }
        this.$http.post(url, qs.stringify(data), {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(res => {
          if (res.data.code === 200) {
            Toast({
              message: res.data.data,
              position: 'middle',
              duration: 1500
            })
            this.oponions = ''
            this.getOPinion()
          }
          if (res.data.code === 400) {
            alert('上传失败')
          }
        })
      },
      // 获取意见列表
      getOPinion () {
        const url = `${commonUrl.apihost}index.php/home/personal/getSuggestion`
        this.$http.get(url).then(res => {
          if (res.status === 200) {
            this.lists = res.data.data
//            console.log(this.lists)
          }
        })
      }
    }
  }
</script>
